﻿namespace Task44
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    class Task4
    {
        static void Main()
        {
            string searchedWord = Console.ReadLine();
            byte lines = byte.Parse(Console.ReadLine());

            string[][] text = new string[lines][];

            for (int i = 0; i < lines; i++)
            {
                text[i] = Console.ReadLine().Trim().Split(new char[] { ',', '.', '(', ')', ';', '-', '!', '?', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            }

            int[] counters = new int[lines];

            for (int i = 0; i < lines; i++)
            {
                foreach (var word in text[i])
                {
                    if (String.Compare(word, searchedWord, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        counters[i]++;
                    }
                }
            }

            var dicty = new Dictionary<int, int>();

            for (int i = 0; i < lines; i++)
            {
                dicty[i] = counters[i];
            }

            dicty = dicty.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

            List<List<string>> result = new List<List<string>>();

            foreach (var item in dicty)
            {
                List<string> tempList = new List<string>(text[item.Key]);
                
                result.Add(tempList);
                
            }

            result.Reverse();

            for (int row = 0; row < result.Count; row++)
            {
                for (int word = 0; word < result[row].Count; word++)
                {
                    if (String.Compare(searchedWord, result[row][word], StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        Console.Write(searchedWord.ToUpper());
                    }
                    else
                    {
                        Console.Write(result[row][word]); 
                    }

                    if (word < result[row].Count - 1)
                    {
                        Console.Write(" ");
                    }
                }

                Console.WriteLine();
            }
        }
    }
}